package com.shop.trade.api.service.impl;

import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.OrderBaseInput;
import com.shop.common.model.param.trade.OrderCommentInput;
import com.shop.common.model.param.trade.OrderDetailInput;
import com.shop.common.model.param.trade.SnapshotInput;
import com.shop.common.model.result.trade.OrderCommentOutput;
import com.shop.common.model.result.trade.OrderDetailOutput;
import com.shop.common.model.result.trade.SnapshotOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.DateUtil;
import com.shop.common.util.state.DataStatus;
import com.shop.trade.api.service.IOrderCommentSvc;
import com.shop.trade.atom.atomsvc.*;
import com.shop.trade.feign.item.IItemAtomFeignClientSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年04月16日  3:30
 */
@Service
public class OrderCommentSvc extends BaseService implements IOrderCommentSvc {
    @Autowired
    IOrderCommentAtomSvc iOrderCommentAtomSvc;
    @Autowired
    IOrderAtomSvc iOrderAtomSvc;
    @Autowired
    IOrderDetailAtomSvc iOrderDetailAtomSvc;
    @Autowired
    IItemAtomFeignClientSvc iItemAtomFeignClientSvc;
    @Autowired
    ISnapshotAtomSvc iSnapshotAtomSvc;
    @Autowired
    IOrderLogAtomSvc iOrderLogAtomSvc;

    @Override
    public ServiceBean<Integer> addComment(OrderCommentInput input, Integer userId) {
        OrderBaseInput orderBaseInput = new OrderBaseInput();
        orderBaseInput.setId(input.getOrderId());
        OrderDetailInput orderDetailInput = new OrderDetailInput();
        orderDetailInput.setItemId(input.getItemId());
        orderDetailInput.setOrderId(input.getOrderId());
        orderDetailInput.setBuyerId(userId);
        ServiceBean<List<OrderDetailOutput>> serviceBean = iOrderDetailAtomSvc.selectOrderDetailList(orderDetailInput);
        if (serviceBean.isFaild()) {
            return resultFailed("该商品不存在");
        }
        OrderDetailOutput orderDetailOutput = serviceBean.getData().get(0);
        if (orderDetailOutput.getIscomment() == 1) {
            return resultFailed("该商品已经评论过了");
        }
        orderDetailInput = new OrderDetailInput();
        orderDetailInput.setId(orderDetailOutput.getId());
        orderDetailInput.setIscomment(1);
        iOrderDetailAtomSvc.updateOrderDetails(orderDetailInput);
        SnapshotInput snapshotInput = new SnapshotInput();
        snapshotInput.setOrderId(input.getOrderId());
        snapshotInput.setItemId(input.getItemId());
        SnapshotOutput snapshotOutput = iSnapshotAtomSvc.qrySnapshot(snapshotInput).getData();

        input.setItemName(snapshotOutput.getItemName());
        input.setItemImg(snapshotOutput.getMainImg());
        input.setMchtId(orderDetailOutput.getMchtId());
        input.setBuyerId(orderDetailOutput.getBuyerId());
        input.setOpId(userId);
        input.setCreateTime(DateUtil.newNowDate());
        input.setDataStatus(DataStatus.USE.value());
        return iOrderCommentAtomSvc.addComment(input);
    }

    @Override
    public ServiceBean<Integer> modComment(OrderCommentInput input) {
        input.setModTime(DateUtil.newNowDate());
        return iOrderCommentAtomSvc.modComment(input);
    }

    @Override
    public ServiceBean<Integer> delComment(Integer id) {
        return iOrderCommentAtomSvc.delComment(id);
    }

    @Override
    public ServiceBean<OrderCommentOutput> qryOrderComment(OrderCommentInput input) {
        return iOrderCommentAtomSvc.qryOrderComment(input);
    }

    @Override
    public ServiceBean<List<OrderCommentOutput>> qryOrderComments(OrderCommentInput input) {
        return iOrderCommentAtomSvc.qryOrderComments(input);
    }

    @Override
    public ServiceBean<List<OrderCommentOutput>> qryOrderComments4P(InputBean<OrderCommentInput> input) {
        return iOrderCommentAtomSvc.qryOrderComments4P(input);
    }

    @Override
    public ServiceBean<OrderCommentOutput> qryCommentCount(OrderCommentInput input) {
        return resultData(iOrderCommentAtomSvc.qryCommentCount(input));
    }
}
